package com.dhu.service.impl;

import com.dhu.dao.PostMapper;
import com.dhu.dao.ReplyInnerMapper;
import com.dhu.dao.ReplyMapper;
import com.dhu.pojo.Reply;
import com.dhu.pojo.ReplyInner;
import com.dhu.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private ReplyInnerMapper replyInnerMapper;

    // 查询回复
    @Override
    public List<Reply> selectByPostId(int id) {
        try {
            List<Reply> replies = replyMapper.selectByPostId(id);
            for (Reply reply : replies) {
                List<ReplyInner> replyInners = replyInnerMapper.selectByReplyId(reply.getId());
                reply.setReplyInnerList(replyInners);
            }
            return replies;
        } catch (Exception e) {
            return null;
        }
    }

    // 增加回复
    @Override
    public int addReply(Reply reply) {
        try {
            // 帖子的回复数+1
            postMapper.addOneReplyNum(reply.getPostId());
            return replyMapper.addReply(reply);
        } catch (Exception e) {
            return -1;
        }
    }
}
